//
//  MLABaseFieldView.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/18.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLABaseFieldView.h"

@interface MLABaseFieldView () <UITextFieldDelegate>

@end

@implementation MLABaseFieldView {
    UILabel *_titleLbl;
    UITextField *_textField;
    UIImageView *_accessImgv;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = CX_GetColorHex(0xFCFCFC, 1);
        
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.textColor = CX_GetColorSubTitle();
        _titleLbl.font = CX_GetNormalFont(14);
        [self addSubview:_titleLbl];
        [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.height.mas_equalTo(15);
            make.top.equalTo(self);
        }];
        
        _textField = [[UITextField alloc] init];
        _textField.textColor = CX_GetColorHex(0x333333, 1);
        _textField.font = CX_GetNormalFont(10);
        _textField.backgroundColor = CX_GetColorHex(0xF0F1F4, 1);
        _textField.layer.cornerRadius = 5;
        _textField.layer.masksToBounds = YES;
        _textField.delegate = self;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        _textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        [_textField addTarget:self action:@selector(textDidChanged:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_textField];
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self->_titleLbl);
            make.height.mas_equalTo(40);
            make.right.mas_equalTo(-15);
            make.top.equalTo(self->_titleLbl.mas_bottom).offset(10);
        }];
        
        _accessImgv = [[UIImageView alloc] init];
        _accessImgv.image = [UIImage imageNamed:@"right_arrow_icon"];
        _accessImgv.hidden = YES;
        [self addSubview:_accessImgv];
        [_accessImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_equalTo(9);
            make.centerY.equalTo(self->_textField);
            make.right.equalTo(self->_textField).offset(-8);
        }];
        
        _maxLength = 30;
        _canEdit = YES;
        _secureTextEntry = NO;
        _canSelect = NO;
    }
    return self;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    _titleLbl.text = title;
}

- (void)setInputText:(NSString *)inputText {
    _inputText = inputText;
    _textField.text = _inputText;
}

- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    _textField.placeholder = _placeHolder;
}

- (void)setMaxLength:(NSInteger)maxLength {
    _maxLength = maxLength;
}

- (void)setSecureTextEntry:(BOOL)secureTextEntry {
    _secureTextEntry = secureTextEntry;
    _textField.secureTextEntry = _secureTextEntry;
}

- (void)setCanEdit:(BOOL)canEdit {
    _canEdit = canEdit;
}

- (void)setCanSelect:(BOOL)canSelect {
    _canSelect = canSelect;
    _accessImgv.hidden = !_canSelect;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType {
    _keyboardType = keyboardType;
    _textField.keyboardType = _keyboardType;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!_canEdit) {
        if (_clickActionBlock) {
            _clickActionBlock();
        }
    }
    return _canEdit;
}

- (void)textDidChanged:(UITextField *)textField {
    if (textField.text.length > _maxLength) {
        textField.text = [textField.text substringToIndex:_maxLength];
    }
    _inputText = textField.text;
}

- (void)setModel:(MLABaseFieldModel *)model {
    _model = model;
    
    _titleLbl.text = _model.title;
    _textField.placeholder = _model.placeHolder;
    _textField.secureTextEntry = _model.secureTextEntry.boolValue;
    _textField.text = _model.inputText;
    _inputText = _model.inputText;
    _accessImgv.hidden = !_model.canSelect.boolValue;
    _canEdit = _model.canEdit.boolValue;
    _maxLength = _model.maxLength.integerValue;
}


@end
